/********页面操作*******/
var global = {};//全局对象，共享数据
global.host = "http://123.206.18.189:8080/";//设置主机
var questionId = sessionStorage.getItem("questionId");
var token = sessionStorage.getItem("token");
$.ajax({
    type:"GET",
    url:"http://123.206.18.189:8080/question/"+questionId,
    headers:{
        authToken:token
    },
    dataTpye:"json",
    contentType:"application/json",
    success:function (res) {
      	update_callback(res);
    }
})

function update_callback(res) {
	var question = new Question();
	question.setPaperId(res.examinationPaperId);
	var qEditor = new QuestionEditor(question,"timu","#answers-list");
	qEditor.setTitle(res.title);
	$("#extra_info").val(res.info);
	// 绑定选择题型
	$("#question_type_group button.btn").click(function(){
	    $("#question_type_group button.btn").removeClass("btn-primary");
	    $(this).addClass("btn-primary");
	    var type = $(this).attr("attr_value");
	    question.setType(type);
	    var options = res.options;
	    for(let i = 0; i < options.length; i++) {
	    	 qEditor.setOption(options[i]);
	    }
	});

	question.setType(res.type);
	if(res.type == "single") {
		//模拟点击事件，初始化为单选类型
		$("#single_select_btn").click();
	} else if(res.type == "multi") {
		$("#multi_select_btn").click();
	} else {
		return alert("题目类型错误");
	}

	/**
	 * 添加选项
	 */
	$("#add-answer").click(function(){
	    qEditor.addOption();
	});


	function dealChangeQuestion(xhr,msg){
	    if(xhr.status == 200 && xhr.readyState == 4){
	        alert("试题修改成功！")
	    }else{
	        alert("试题修改失败！")
	    }
	}

	//修改提交页面
	$("#question_change_btn").click(function(){
		console.info(qEditor);
	    // let question = qEditor.question;
	    var questionId = qEditor.titleId;
	    var title = qEditor.getTextValue("#"+questionId);
	    if(qEditor.isEmpty(title)){
	        return alert("题目不能为空！");
	    }
	    //设置题目
	    question.setTitle(title);
	    var options = question.options;
	    var optionNumber = 0;
	    console.info(options);
	    for(let key in options) {
	        var option = options[key];
	        var opValue = qEditor.getTextValue("#"+ option.id);
	        if(qEditor.isEmpty(opValue)) {
	            option.setContent("");
	        } else {
	            option.setContent(opValue);
	            optionNumber++;
	        }
	        var score = $("#" +option.scoreId).val().trim();
	        if(score.length > 0){
	            if(/^[0-9]*$/.test(score)) {
	                option.setScore(Number(score));
	            } else {
	                alert("分数必须是数字！");
	                return ;
	            }
	        }
	    }

	    if(optionNumber < 1) {
	        if(!confirm("确定该实体没有选项吗？")){
	            return false;
	        }
	    }
	    var pureQuestion = qEditor.getPureQuestion();
	    var url = global.host + "question";
	    putJson(url,pureQuestion,dealChangeQuestion);
	    // console.info(pureQuestion);
	});
	/**
	 * 提交数据
	 * @param url  请求地址
	 * @param postData  提交的数据
	 * @param method  提交的方式
	 * @param callback 回调函数，用于处理后继请求
	 */
	function postJson(url,postData,callback) {
	    $.ajax({
	        type: "PUT",
	        url: url,
	        headers:{
	          authToken:sessionStorage.getItem("token")
	        },
	        contentType: "application/json; charset=utf-8",
	        data: JSON.stringify(postData),
	        complete: function(xhr,msg){
	            callback(xhr,msg);
	        }
	    });
	}
	function putJson(url,putData,callback) {
	    $.ajax({
	        type: "PUT",
	        url: url,
	        headers:{
	            authToken:sessionStorage.getItem("token")
	        },
	        contentType: "application/json; charset=utf-8",
	        data: JSON.stringify(putData),
	        complete: function(xhr,msg){
	            callback(xhr,msg);
	        }
	    });
	}
}



//初始化题目
// qEditor.initTitle();

